package subject_set.top101;

/**
 * @author haomin
 * @date 2022/11/02 22:05
 **/
public class BM38 {
    public class Solution {
        public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
            return dfs(root, o1, o2).val;
        }
        private TreeNode dfs(TreeNode root, int p, int q){
            if(root == null) return root;
            if(root.val == p || root.val == q) return root;
            TreeNode l = dfs(root.left, p, q);
            TreeNode r = dfs(root.right, p, q);
            if(l == null && r == null) return null;
            if(l == null) return r;
            if(r == null) return l;
            return root;
        }
    }
}